package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.SectorForm;
import com.vcaresolution.logistic.pojo.City;
import com.vcaresolution.logistic.pojo.Country;
import com.vcaresolution.logistic.pojo.Sector;
import com.vcaresolution.logistic.pojo.State;
import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.to.SectorTO;

@Service
public class SectorServiceImpl extends BaseService implements SectorService{

	private Sector populateSectorPojo(SectorForm sectorForm){
		Sector sector = new Sector();
		BeanUtils.copyProperties(sectorForm, sector);
		
		return sector;
	}
	
	@Override
	@Transactional
	public void add(SectorForm sectorForm) {
		Sector sector = populateSectorPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().add(sector);
	}

	@Override
	@Transactional
	public void update(SectorForm sectorForm) {
		Sector sector = populateSectorPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().update(sector);
	}

	@Override
	@Transactional
	public void delete(SectorForm sectorForm) {
		getDaoFactory().getUtilityDAO().delete(populateSectorPojo(sectorForm));
	}

	@Override
	@Transactional
	public SectorTO findById(Serializable sectorID) {
		Sector sector = (Sector) getDaoFactory().getUtilityDAO().findById(Sector.class,sectorID);
		
		SectorTO sectorTO = new SectorTO();
		BeanUtils.copyProperties(sector, sectorTO);
		
		//TODO - TESTING PURPOSE
		getAssociatedCountryList(sectorID);
		
		return sectorTO; 
	}

	@Override
	@Transactional
	public List<SectorTO> findByAll() {
		List<Sector> sectorList = getDaoFactory().getUtilityDAO().findByAll(Sector.class);
		
		List<SectorTO> sectorTOList = new ArrayList<SectorTO>();
		
		if( sectorList != null && !sectorList.isEmpty() ){
			SectorTO sectorTO = null;
			for(Sector sector: sectorList){
				sectorTO = new SectorTO();
				
				BeanUtils.copyProperties(sector, sectorTO);
				sectorTOList.add(sectorTO);
			}
		}
		
		return sectorTOList;
	}

	@Override
	@Transactional
	public List<DataHolderTO> getAssociatedCountryList(Serializable sectorID) {
		List<DataHolderTO> countryTOList = new ArrayList<DataHolderTO>();
		
		List<Country> countryList = getDaoFactory().getSectorDAO().getAssociatedCountryList(sectorID);
		if( countryList != null && !countryList.isEmpty() ){
			DataHolderTO countryTO = null;
			for(Country country : countryList){
				countryTO = new DataHolderTO();
				
				countryTO.setFieldId(country.getCountryid());
				countryTO.setFieldname(country.getCountryname());
				
				countryTOList.add(countryTO);
			}
		}
		
		return countryTOList;
	}
	
	@Override
	@Transactional
	public List<DataHolderTO> getAssociatedStateList(Serializable countryID) {
		List<DataHolderTO> stateTOList = new ArrayList<DataHolderTO>();
		
		List<State> stateList = getDaoFactory().getSectorDAO().getAssociatedStateList(countryID);
		if( stateList != null && !stateList.isEmpty() ){
			DataHolderTO stateTO = null;
			for(State state : stateList){
				stateTO = new DataHolderTO();
				
				stateTO.setFieldId(state.getStateid());
				stateTO.setFieldname(state.getStatename());
				
				stateTOList.add(stateTO);
			}
		}
		
		return stateTOList;
	}
	
	@Override
	@Transactional
	public List<DataHolderTO> getAssociatedCityList(Serializable stateID) {
		List<DataHolderTO> cityTOList = new ArrayList<DataHolderTO>();
		
		List<City> cityList = getDaoFactory().getSectorDAO().getAssociatedCityList(stateID);
		if( cityList != null && !cityList.isEmpty() ){
			DataHolderTO cityTO = null;
			for(City city : cityList){
				cityTO = new DataHolderTO();
				
				cityTO.setFieldId(city.getCityid());
				cityTO.setFieldname(city.getCityname());
				
				cityTOList.add(cityTO);
			}
		}
		
		return cityTOList;
	}

}
